/*OWmq6Ugo6jPE4W7xoi1UXhNbCE1ElH1XXFf2Qv95OS2hz8AZIpqhqTmMbTjApSmV*/
import { ajax, toast, promptBox } from 'nc-lightapp-front';
import { currentTypeAfterFormEvents } from '../../../../public/components/pubUtils/currentTypeAfterEvent';
import { formulamsgHint, renderData, headAfterEventRenderData, bodyAfterEventRenderData, errorDeal } from '../../../../public/components/afterEventPub/afterEventPubDeal';
import { autoAddLineKeys } from '../../../../public/components/pubUtils/billPubInfo';
import { getColvalues, getRowIds, refreshChildVO2HeadVO } from '../../../../public/components/pubUtils/billPubUtil';
import { moneyAndRateFields } from '../../../../public/components/pubUtils/specialFieldAfterEvent.js';
import { isExistsTopBill } from '../../../../public/components/pubUtils/billPubUtil.js';

export default function afterEvent(props, moduleId, key, value, changedrows, i, s, g) {
	if (changedrows instanceof Array) {
		if (changedrows[0].newvalue.value == changedrows[0].oldvalue.value) {
			return;
		}
	}
	let pagecode = this.getPagecode();
	let index = 0;
	if (moduleId == this.formId) {
		index = 0;
	} else if (moduleId == this.tableId) {
		index = i;
	}
	//表头编辑后事件
	if (moduleId == this.formId) {
		let data = null
		switch (key) {
			case 'pk_org_v':
				if (value.value == null || value.value == '') {
					//清空财务组织
					if (this.props.getUrlParam('type') === 'transfer') {
						promptBox({
							color: 'warning',
							title: this.state.json['payablebill-000033'] /* 国际化处理： 确认修改*/,
							content: this.state.json['payablebill-000064'] /* 国际化处理： 来源于上游的单据不允许清空财务组织！*/,
							beSureBtnName: this.state.json['payablebill-000040'] /* 国际化处理： 确认*/,
							noCancelBtn: true,
							beSureBtnClick: () => {
								this.props.form.setFormItemsValue(this.formId, { pk_org_v: changedrows });
							},
							closeByClickBackDrop: false
						});
					} else {
						promptBox({
							color: 'warning',
							title: this.state.json['payablebill-000033'] /* 国际化处理： 确认修改*/,
							content: this.state.json['payablebill-000034'] /* 国际化处理： 确定​修改组织，这样会清空您录入的信息?*/,
							beSureBtnName: this.state.json['payablebill-000040'] /* 国际化处理： 确认*/,
							cancelBtnName: this.state.json['payablebill-000009'] /* 国际化处理： 取消*/,
							beSureBtnClick: () => {
								this.props.form.EmptyAllFormValue(this.formId);
								this.props.cardTable.setTableData(this.tableId, { rows: [] });
								this.initAdd(true);
							},
							cancelBtnClick: () => {
								this.props.form.setFormItemsValue(this.formId, { pk_org_v: changedrows });
							},
							closeByClickBackDrop: false
						});
					}
				} else if (changedrows.value != null && changedrows.value != '') {
					//切换组织
					promptBox({
						color: 'warning',
						title: this.state.json['payablebill-000033'] /* 国际化处理： 确认修改*/,
						content: this.state.json['payablebill-000034'] /* 国际化处理： 确定​修改组织，这样会清空您录入的信息?*/,
						beSureBtnName: this.state.json['payablebill-000040'] /* 国际化处理： 确认*/,
						cancelBtnName: this.state.json['payablebill-000009'] /* 国际化处理： 取消*/,
						beSureBtnClick: () => {
							ajax({
								url: '/nccloud/arap/payablebill/cardheadafteredit.do',
								async: false,
								data: {
									pageId: pagecode,
									event: this.props.createHeadAfterEventData(pagecode, this.formId, this.tableId, moduleId, key, value),
									uiState: this.props.getUrlParam('status')
								},
								success: (res) => {
									//渲染数据
									renderData(this, res);
									//编辑公式提示
									formulamsgHint(this, res);
								},
								error: (res) => {
									errorDeal(this, res, changedrows, key);
								}
							});
						},
						cancelBtnClick: () => {
							this.props.form.setFormItemsValue(this.formId, { pk_org_v: changedrows });
						},
						closeByClickBackDrop: false
					});
				} else {
					data = {
						pageId: pagecode,
						event: this.props.createHeadAfterEventData(pagecode, this.formId, this.tableId, moduleId, key, value),
						uiState: this.props.getUrlParam('status')
					}
					headFieldAfterRequest.call(this, data, key, changedrows)
				}
				break;
			case 'supplier_v':
				data = {
					pageId: pagecode,
					formEvent: props.createFormAfterEventData(pagecode, this.formId, this.tableId, key, value),
					colValues: getColvalues(this.props, this.tableId, ['pk_org', 'pk_org_v', 'payaccount', 'recaccount', 'pk_currtype', 'supplier', 'supplier_v', 'buysellflag',
						'objtype', "direction", "pk_billtype", "top_billtype", "pk_payterm", "pk_psndoc", "pk_deptid", "pk_deptid_v",'pk_ratetype','ratedate','ordercubasdoc','ordercubasdoc_v'].concat(moneyAndRateFields)),
					rowids: getRowIds(this.props, this.tableId),
					uiState: this.props.getUrlParam('status')
				};
				headFieldAfterRequest.call(this, data, key, changedrows)
				break;
			case 'payaccount':
				data = {
					pageId: pagecode,
					formEvent: props.createFormAfterEventData(pagecode, this.formId, this.tableId, key, value),
					colValues: getColvalues(this.props, this.tableId, ['pk_org', 'pk_org_v', 'payaccount', 'pk_currtype', 'supplier', 'supplier_v', 'buysellflag',
						'objtype', "direction", "pk_billtype"].concat(moneyAndRateFields)),
					rowids: getRowIds(this.props, this.tableId),
					uiState: this.props.getUrlParam('status')
				};
				headFieldAfterRequest.call(this, data, key, changedrows)
				break;
			case 'pk_deptid_v':
				data = {
					pageId: pagecode,
					formEvent: props.createFormAfterEventData(pagecode, this.formId, this.tableId, key, value),
					colValues: getColvalues(this.props, this.tableId, ['pk_org', 'pk_org_v', 'pk_deptid', 'pk_deptid_v', 'pk_currtype', 'supplier', 'supplier_v', 'taxtype', 'buysellflag', 'objtype', "direction", 'pk_psndoc','pk_pcorg','pk_pcorg_v','billdate']),
					rowids: getRowIds(this.props, this.tableId),
					uiState: this.props.getUrlParam('status')
				};
				headFieldAfterRequest.call(this, data, key, changedrows)
				break;
			case 'pk_psndoc':
				data = {
					pageId: pagecode,
					formEvent: props.createFormAfterEventData(pagecode, this.formId, this.tableId, key, value),
					colValues: getColvalues(this.props, this.tableId, ['pk_org', 'pk_org_v', 'pk_psndoc', 'pk_deptid', 'pk_deptid_v', 'isrefused', 'prepay', 'isdiscount', 'objtype', 'direction', 'agentreceivelocal', 'buysellflag','pk_pcorg','pk_pcorg_v']),
					rowids: getRowIds(this.props, this.tableId),
					uiState: this.props.getUrlParam('status')
				};
				headFieldAfterRequest.call(this, data, key, changedrows)
				break;
			case 'pk_currtype':
				data = {
					pageId: pagecode,
					formEvent: props.createFormAfterEventData(pagecode, this.formId, this.tableId, key, value),
					colValues: getColvalues(this.props, this.tableId, ["pk_org", "pk_org_v", "pk_group", "pk_currtype", "pk_billtype", "billdate", "rate", "grouprate", "globalrate",
						"buysellflag", "taxprice", "local_taxprice", "taxrate", "occupationmny", "money_bal", "local_money_bal",
						"globalcrebit", "globalnotax_cre", "globaltax_cre", "groupcrebit", "groupnotax_cre", "grouptax_cre",
						"local_money_cr", "local_notax_cr", "local_tax_cr", "money_cr", "notax_cr", "quantity_cr", "direction", "settlecurr", "settlemoney", "pk_ratetype", "ratedate","top_billtype","settlemoney","settlecurr","supplier_v","supplier"]),
					rowids: getRowIds(this.props, this.tableId),
					uiState: this.props.getUrlParam('status')
				}
				headFieldAfterRequest.call(this, data, key, changedrows)
				//币种事件发送完要判断汇率
				//v2111 汇率控制发生变化，都走编辑前进行控制 (this.formId, props, key);
				break;
			case 'subjcode':
				data = {
					pageId: pagecode,
					event: this.props.createHeadAfterEventData(pagecode, this.formId, this.tableId, moduleId, key, value),
					uiState: this.props.getUrlParam('status')
				}
				headFieldAfterRequest.call(this, data, key, changedrows)
				// this.props.form.setFormItemsValue(this.formId, { 'subjcode': { value: value.refpk, display: value.dispname } });
				break;
			case 'pk_ratetype':
				data = {
					pageId: pagecode,
					event: this.props.createHeadAfterEventData(pagecode, this.formId, this.tableId, moduleId, key, value),
					uiState: this.props.getUrlParam('status')
				}
				if (value.value == null || value.value == '') {
					promptBox({
						color: 'warning',
						title: this.state.json['payablebill-000068'] /* 国际化处理： 确认清空*/,
						content: this.state.json['payablebill-000069'] /* 国际化处理： 确定​清空汇率类型，这样会清空金额相关的信息！*/,
						beSureBtnName: this.state.json['payablebill-000040'] /* 国际化处理： 确定*/,
						cancelBtnName: this.state.json['payablebill-000009'] /* 国际化处理： 取消*/,
						beSureBtnClick: () => {
							headFieldAfterRequest.call(this, data, key, changedrows)
						},
						cancelBtnClick: () => {
							this.props.form.setFormItemsValue(this.formId, { pk_ratetype: changedrows });
						},
						closeByClickBackDrop: false
					});
				} else {
					headFieldAfterRequest.call(this, data, key, changedrows)
				}
				break;
			default:
				data = {
					pageId: pagecode,
					event: this.props.createHeadAfterEventData(pagecode, this.formId, this.tableId, moduleId, key, value),
					uiState: this.props.getUrlParam('status')
				}
				headFieldAfterRequest.call(this, data, key, changedrows)
				break;
		}
	}

	//表体编辑后事件
	if (moduleId == this.tableId) {
		//科目字段特殊拼接1121\应收票据
		if (key == "subjcode") {
			this.props.cardTable.setValByKeyAndIndex(this.tableId, i, 'subjcode', { value: value.refpk, display: value.dispname })
		}

		ajax({
			url: '/nccloud/arap/payablebill/cardbodyafteredit.do',
			data: {
				rowindex: 0,
				editindex: index,
				pageId: pagecode,
				changedrows: changedrows,
				tableId: this.tableId,
				body: props.cardTable.getDataByIndex(this.tableId, index),
				formEvent: props.createFormAfterEventData(pagecode, this.formId, this.tableId, key, value),
				uiState: this.props.getUrlParam('status')
			},
			async: false,
			success: (res) => {
				//渲染数据
				bodyAfterEventRenderData(this, res);

				if (this.props.getUrlParam('type') == 'transfer') {
					this.synTransferData();
				}
				//表体改变表头税率编辑性
				if (i == 0 && key == 'pk_currtype') {
					//v2111 汇率控制发生变化，都走编辑前进行控制 (this.formId, props, 'pk_currtype');
				}
				//编辑公式提示
				formulamsgHint(this, res);
			},
			error: (res) => {
				let str = res.message;
				if (str.substring(0, 16) == 'convertException') {
					promptBox({
						color: 'warning',
						title: this.state.json['payablebill-000004'] /* 国际化处理： 折算误差*/,
						content: str.substring(16, str.length),
						closeByClickBackDrop: false,
						beSureBtnClick: () => {
							afterTableEvent(this, props, i, pagecode, moduleId, key, changedrows, 'sure', index, value);
						},
						cancelBtnClick: () => {
							afterTableEvent(this, props, i, pagecode, moduleId, key, changedrows, 'cancel', index, value);
						}
					});
				} else {
					this.props.cardTable.setValByKeyAndRowId(this.tableId, i, key, changedrows);
					toast({ color: 'danger', content: str });;
				}
			}
		});

		if(key == "ytdrow"){
			//修改金额
			let body = props.cardTable.getDataByIndex(this.tableId, index);
			let oldMoney=body.values.local_money_cr.value;
			let money = oldMoney;
			let isChangeMoney = false;
			ajax({url:'/nccloud/arap/yt/getMoney.do',
				data:{value:value},
				async:false,
				success:(res)=>{
					money = res.data.money;
					isChangeMoney=true;
				},
				error:(res)=>{
					console.log(res);
				}
			})

			if(isChangeMoney){
				let moneyChangedrows = changedrows;
				moneyChangedrows[0].oldvalue.value=oldMoney;
				moneyChangedrows[0].oldvalue.scale=2;
				moneyChangedrows[0].newvalue.value=money;
				moneyChangedrows[0].newvalue.scale=2;
				/**
				 * 表格id bodys
				 * 行号 0
				 * 字段编码 local_money_cr
				 * 字段值（对象） {"value":"100.00","display":null}
				 */
				this.props.cardTable.setValByKeyAndIndex(this.tableId,i,'local_money_cr',{"value":money})
				ajax({
					url: '/nccloud/arap/payablebill/cardbodyafteredit.do',
					data: {
						rowindex: 0,
						editindex: index,
						pageId: pagecode,
						changedrows: moneyChangedrows,
						tableId: this.tableId,
						body: props.cardTable.getDataByIndex(this.tableId, index),
						formEvent: props.createFormAfterEventData(pagecode, this.formId, this.tableId, "local_money_cr", money),
						uiState: this.props.getUrlParam('status')
					},
					async: false,
					success: (res) => {
						//渲染数据
						bodyAfterEventRenderData(this, res);
			
						if (this.props.getUrlParam('type') == 'transfer') {
							this.synTransferData();
						}
						//编辑公式提示
						formulamsgHint(this, res);
					},
					error: (res) => {
						let str = res.message;
						if (str.substring(0, 16) == 'convertException') {
							promptBox({
								color: 'warning',
								title: this.state.json['payablebill-000004'] /* 国际化处理： 折算误差*/,
								content: str.substring(16, str.length),
								closeByClickBackDrop: false,
								beSureBtnClick: () => {
									afterTableEvent(this, props, i, pagecode, moduleId, "local_money_cr", moneyChangedrows, 'sure', index, money);
								},
								cancelBtnClick: () => {
									afterTableEvent(this, props, i, pagecode, moduleId, "local_money_cr", moneyChangedrows, 'cancel', index, money);
								}
							});
						} else {
							this.props.cardTable.setValByKeyAndRowId(this.tableId, i, "local_money_cr", moneyChangedrows);
							toast({ color: 'danger', content: str });;
						}
					}
				});
			}
			
		}

		//自动增行
		autoAddline.call(this, moduleId, pagecode, key, i)
	}


}

function afterTableEvent(that, props, i, pagecode, moduleId, key, changedrows, isCalculateConvert, index, value) {

	ajax({
		url: '/nccloud/arap/payablebill/cardbodyafteredit.do',
		data: {
			rowindex: 0,
			editindex: index,
			pageId: pagecode,
			changedrows: changedrows,
			tableId: that.tableId,
			body: props.cardTable.getDataByIndex(that.tableId, index),
			formEvent: props.createFormAfterEventData(pagecode, that.formId, that.tableId, key, value),
			uiState: that.props.getUrlParam('status'),
			isCalculateConvert: isCalculateConvert
		},
		async: false,
		success: (res) => {
			//渲染数据
			bodyAfterEventRenderData(that, res);

			if (that.props.getUrlParam('type') == 'transfer') {
				that.synTransferData();
			}
			//表体改变表头税率编辑性
			if (i == 0 && key == 'pk_currtype') {
				//v2111 汇率控制发生变化，都走编辑前进行控制 currentTypeAfterFormEvents(that.formId, props, 'pk_currtype');
			}
			//编辑公式提示
			formulamsgHint(that, res);
		}
	});
}

export function headFieldAfterRequest(requestData, key, changedrows) {
	ajax({
		url: '/nccloud/arap/payablebill/cardheadafteredit.do',
		data: requestData,
		async: false,
		success: (res) => {
			//渲染数据
			headAfterEventRenderData(this, res);
			//编辑公式提示
			formulamsgHint(this, res);

			if (key == 'pk_org_v') {
				let pk_org_v = this.props.form.getFormItemsValue(this.formId, 'pk_org_v').value;
				if (pk_org_v) {
					this.props.resMetaAfterPkorgEdit();
					this.state.buttonfalg = true;
				} else {
					this.state.buttonfalg = null;
				}
				this.toggleShow();
			}

			if (this.props.getUrlParam('type') == 'transfer') {
				this.synTransferData();
			}
		},
		error: (res) => {
			errorDeal(this, res, changedrows, key);
		}
	});
}

export function autoAddline(moduleId, pagecode, key, i) {
	//自动增行
	let allRowsNumber = this.props.cardTable.getNumberOfRows(this.tableId);
	if (moduleId == this.tableId && allRowsNumber == i + 1 && autoAddLineKeys.indexOf(key) != -1 && !isExistsTopBill(this)) {
		let data = this.props.createMasterChildData(pagecode, this.formId, this.tableId);
		//清空cardData的表体
		data.body[this.tableId].rows = [];
		ajax({
			url: '/nccloud/arap/payablebillpub/addline.do',
			data: data,
			async: false,
			success: (res) => {
				if (res.data) {
					if (res.data.head) {
						this.props.form.setAllFormValue({ [this.formId]: res.data.head[this.formId] });
					}
					if (res.data.body) {
						this.props.cardTable.addRow(this.tableId, allRowsNumber, res.data.body[this.tableId].rows[0].values, false);
					}
				}
			}
		});
	}
}


/*OWmq6Ugo6jPE4W7xoi1UXhNbCE1ElH1XXFf2Qv95OS2hz8AZIpqhqTmMbTjApSmV*/